#ifndef DKM_ENGINE_H
#define DKM_ENGINE_H

#include <algorithm>
#include <vector>
#include "../D3DUtil.h"
#include "Component.h"

using namespace std;

namespace dkm
{
	//Singleton: Engine can only be instantiated once.
	class Engine
	{
	public:
		static Engine* GetInstance()
		{
			if (NULL == instance)
			{
				instance = new Engine();
			}
			return instance;
		}

		static void Kill()
		{
			if(NULL != instance)
			{
				delete instance;
				instance = NULL;
			}
		}

		void Update(float dt);
		void Draw();

		//Engine iterates through a list of Components 
		//to call the draw and update functions.
		void AddComponent(Component* component);
		void RemoveComponent(Component* component);

		//Functions to add, remove or use another device.
		void AddDevice(IDirect3DDevice9* device, bool useNow);
		void RemoveDevice(IDirect3DDevice9* device);
		void UseDevice(IDirect3DDevice9* device);

		//Functions to manage the textures
		//Textures are stored in an array.
		//You can add the texture and return the added texture.
		//If the texture already exists, it only returns the texture.
		IDirect3DTexture9* AddTex(string texName);
		void RemoveTex(string texName);
		bool FindTex(string texName);

		//Returns the index of the texture in the array or -1 if not found.
		int GetTexId(string texName);
		//Returns the texture.
		IDirect3DTexture9* GetTex(string texName);
		
		//Calls ReleaseCOM on all the textures
		void CleanAllTex();

	private:
		Engine();
		~Engine();

		static Engine* instance;

		int deviceToUse;
		std::vector<IDirect3DDevice9*> devices;
		std::vector<string> texNames;
		std::vector<IDirect3DTexture9*> allTex;
		std::vector<Component*> allComponents;		
	};
}

#endif